<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI

class Home_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('result_model', '', 'TRUE');
        $this->load->model('user', '', true);
        if (!$this->session->userdata('logged_in')) {
            redirect('user/login', 'refresh');
        }
        
        $this->load->library('authentication');$this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();$this->authentication->redirectIfNotBe( get_employee_titles());
        
    }

    function index() {
        
        $user = $this->session->userdata('logged_in');
        $this->load->model('quiz_model', '', true);
        
        $quiz = $this->quiz_model->get_last_of_group($user['gid']);
                
        if ($quiz != false)
        {
            redirect('user/quizzes/attempt/' . $quiz['quid']);
        }
        else {
            $this->load->view('user/no_quiz');
        }
        
        
//        $session_data = $this->session->userdata('logged_in');
//        $data['username'] = $session_data['username'];
//        if ($session_data['su'] != "1") {
//            $user_id = $session_data['id'];
//            $result = $this->result_model->user_last_ten_results($user_id);
//            $value = array();
//            $value[] = array('Quiz Name', 'Percentage (%)');
//            foreach ($result as $val) {
//                $value[] = array($val['quiz_name'], intval($val['percentage']));
//            }
//            $data['title'] = "Home";
//            $data['value'] = json_encode($value);
//            $data['LoggedUser'] = $this->LoggedUser;
//            
//            
//            //$this->load->view('general/header', $data);
//            $this->load->view('user/home_view', $data);
//            //$this->load->view('general/footer', $data);
//        } else {
//            $data['user_group'] = $this->user->user_by_group();
//            $data['title'] = "Home";
//            //$this->load->view('general/header', $data);
//            $this->load->view('user/home_view_admin', $data);
//            //$this->load->view('general/footer', $data);
//        }
    }

    function setting() {
        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }

        $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['username'];
        $data['title'] = "Setting";
        $this->load->view('general/header', $data);
        $this->load->view('general/home_view_admin', $data);
        $this->load->view('general/footer', $data);
    }

    function logout() {
        $this->load->helper('cookie');
        $this->session->unset_userdata('logged_in');
        $institute_id = $this->session->userdata('institute_id');
        $this->session->sess_destroy();
        session_destroy();
        delete_cookie("rid");
        redirect('login/index/' . urlencode(base64_encode($institute_id)), 'refresh');
    }

}
?>


